(function (win) {
    function ImagePosition(opts) {
        this.defaultOPts = {
            imgContent:'',
            initWid:'',
            initHeight:'',
            data:null,
        }
        for (var key in opts) {
            this.defaultOPts[key] = opts[key];
        }
    }
    ImagePosition.prototype.init = function(opts){
        _this = this;
        for (var key in opts) {
            this.defaultOPts[key] = opts[key];
        }
        var html = '';
        var count = 0;
        var data = this.defaultOPts.data;
        for (var i in data) {
            if (data[i] != undefined){
                var span ='';
                if(parseInt(data[i]['luck_times'])>0){
                    validPeople.push(count);
                    span = '';
                }else{
                    span = '<div class="notValid"></div>';
                }
                count++;
                html += '<li  class="luck-one" ids=' + data[i]['Id'] + ' name=' + data[i]['real_name'] + ' prize=' + i + '>' +
                    span+
                    '<img src="' +locationHref+ data[i]['image'] + '" alt="" onerror="this.src=\'/static/images/faces/1.jpg\'">' +
                    '<span></span>' +
                    '</li>';
            }
        }
        for(var i in validPeople){
            coutmUN[validPeople[i]] = 0;
        }
        this.defaultOPts.imgContent.html(html);
       _this.render();
    }

    ImagePosition.prototype.render = function(){
        var lis = $('.luck-one');
        var len  =lis.length;
        var maxWidth =Math.sqrt(this.defaultOPts.initWid*this.defaultOPts.initHeight/len); //每一个
        var colum = Math.floor(this.defaultOPts.initWid/maxWidth);  //没行多少个
        var clum_len  = this.defaultOPts.initWid-(maxWidth*(colum-1));   //每行最后一个div的大小
        var column_item  =1;
        var hei = this.defaultOPts.initHeight/(Math.floor(len/colum)); //高度
        var leneL = 0;
        var sizes = [];
        for(var i=0;i<lis.length;i++){
            var item  = {
                width:0,
                height:0
            };
            if((i+1)%colum==0){
                item.width = clum_len;
                item.height = hei;
            }else{
                item.width = maxWidth;
                item.height = hei;
            }
            var num = 0;
            if(len%colum!=0){
                 num = len%colum;
            }
            if(column_item==Math.floor(len/colum)||(column_item==Math.floor(len/colum)+1)){
                item.width = this.defaultOPts.initWid/(colum+num);
                item.height = hei;
            }
            if((i+1)%colum==0){
                column_item++;
            }
            leneL++;
            sizes.push(item);
        }
        // Loop through the demos and put them in the page
        for(var i=0;i<lis.length;i++){
            $(lis[i]).css({
                width:sizes[i].width,
                height:sizes[i].height,
            });
        }
    }
    win.ImagePosition = ImagePosition;
})(window);